<?php
class Bed_manage extends CI_Model{

    public function __construct(){

        parent::__construct();
        $this->load->database();
    }

    //床位管理查询方法-总条数
    public function bed_select_all($jobNumber, $name, $department, $room_number){
        //拼接sql语句
        $sj1 = $jobNumber == '' ? ' 1 = 1 ' : " jobNumber LIKE '%".$jobNumber."%'";                 //工号
        $sj2 = $name == '' ? ' AND 1 = 1 ' : " AND name  LIKE '%".$name."%'";                       //姓名
        $sj3 = $department == '' ? ' AND 1 = 1 ' : " AND department LIKE '%".$department."%'";      //部门
        $sj4 = $room_number == '' ? ' AND 1 = 1 ' : " AND RY_number LIKE '%".$room_number . "%'";   //宿舍房间号
        $sql = " SELECT dorm_sslmanage.area_name ,dorm_sslmanage.area_number ,dorm_sslmanage.floor_name ,dorm_sslmanage.floor_number ,dorm_sslmanage.caveNumber ,dorm_sslmanage.dorm_floor ,dorm_roominfor.room_number ,dorm_roominfor.room_type ,dorm_roominfor.room_sex ,dorm_livepeople.bedNumber ,dorm_livepeople.name ,dorm_livepeople.jobNumber ,dorm_livepeople.department ,dorm_livepeople.phone
               FROM dorm_livepeople INNER JOIN dorm_sslmanage ON dorm_sslmanage.area_number = dorm_livepeople.area_number AND dorm_sslmanage.floor_number = dorm_livepeople.floor_number INNER JOIN dorm_roominfor ON dorm_livepeople.area_number = dorm_roominfor.area_number AND dorm_livepeople.floor_number = dorm_roominfor.floor_number AND dorm_livepeople.room_number = dorm_roominfor.room_number
               WHERE " .$sj1 .$sj2 .$sj3 .$sj4;
        $query = $this->db->query($sql, $sj1, $sj2, $sj3, $sj4);
        $all = $query->num_rows();
        return $all;
    }

    //床位管理查询方法
    public function bed_select($jobNumber, $name, $department, $room_number, $limit ,$count){
        //拼接sql
        if($limit != ''){

            $limit1 = ("$limit" - 1) * 10;
        }
        $sj1 = $jobNumber == '' ? ' 1 = 1 ' : " A.jobNumber LIKE '%".$jobNumber."%'";               //工号
        $sj2 = $name == '' ? ' AND 1 = 1 ' : " AND A.RY_name LIKE '%".$name."%'";                   //姓名
        $sj3 = $department == '' ? ' AND 1 = 1 ' : " AND A.department LIKE '%".$department."%'";    //部门
        $sj4 = $room_number == '' ? ' AND 1 = 1 ' : " AND room_number LIKE '%".$room_number."%'";   //宿舍房间号
        $sj5 = " AND A.quitCheck IS NULL ";
        $sj6 = $limit == '' ? ' AND 1 = 1 ' : "Limit " . $limit1 . ",10";
        $sql = " SELECT B.area_name ,B.area_number ,B.floor_name ,B.floor_number ,B.caveNumber ,B.dorm_floor ,C.room_number ,C.room_type ,C.room_sex ,A.bedNumber ,A.name ,A.jobNumber ,A.department ,A.phone ,A.liveDate ,D.bedStatus
        FROM dorm_livepeople AS A INNER JOIN dorm_sslmanage AS B ON B.area_number = A.area_number AND B.floor_number = A.floor_number 
        INNER JOIN dorm_roominfor AS C ON A.area_number = C.area_number AND A.floor_number = C.floor_number AND A.room_number = C.room_number
        INNER JOIN dorm_bed AS D ON A.jobNumber= D.jobNumber AND A.idNumber = D.idNumber
        WHERE " .$sj1 .$sj2 .$sj3 .$sj4 .$sj5 .$sj6;
        $query = $this->db->query($sql, $sj1, $sj2, $sj3, $sj4 ,$sj5 ,$sj6);

        $sql1 = " SELECT B.area_name,B.area_number ,B.floor_name ,B.floor_number ,B.caveNumber ,B.dorm_floor ,C.room_number,C.room_type ,C.room_sex ,A.bedNumber ,A.name ,A.jobNumber ,A.department ,A.phone ,A.liveDate ,D.bedStatus
        FROM dorm_livepeople AS A INNER JOIN dorm_sslmanage AS B ON B.area_number = A.area_number AND B.floor_number = A.floor_number
        INNER JOIN dorm_roominfor AS C ON A.floor_number = C.floor_number AND A.floor_number = C.floor_number AND A.room_number = C.room_number
        INNER JOIN dorm_bed AS D ON A.jobNumber = D.jobNumber AND A.idNumber = D.idNumber
        WHERE " .$sj1 .$sj2 .$sj3 .$sj4 .$sj5;
        $query1 = $this->db->query($sql1, $sj1, $sj2, $sj3, $sj4 ,$sj5);

        $this->db->select('article_description');
        $this->db->where('jobNumber', $jobNumber);
        $query2 = $this->db->get('dorm_article');
        //床位状态查询
        $this->db->select('bedStatus');
        $this->db->where('jobNumber' ,$jobNumber);
        $query_status = $this->db->get('dorm_bed');
        $page = $query->num_rows();
        $totalpage = $query1->num_rows();

        if ($query->num_rows() > 0) {

            if($jobNumber == '' && $name == '' && $department == '' && $room_number == ''){
                //查询成功
                $return_su = array('res' => '40000', 'msg' => '查询成功', 'data' => $query->result(), 'data1' => $query2->result() ,'totalpage' => $count);
                $result = json_encode($return_su);
            } else {
                //查询成功
                $return_su = array('res' => '40000', 'msg' => '查询成功', 'data' => $query->result(), 'data1' => $query2->result() ,'page' => $page ,'totalpage' => $totalpage);
                $result = json_encode($return_su);
            }
        } else {
            //查询失败
            $return_fa = array('res' => '40001', 'msg' => '查询失败', 'data' => '');
            $result = json_encode($return_fa);
        }
        return $result;
    }

    //宿舍空床位查询
    public function bed_select_people($area_number, $floor_number, $room_number){
        if ($area_number == '' || $floor_number == '' || $room_number == '') {
            $return = array('res' => '40002', 'msg' => '请输入必填字段' ,'data' => '');
            $result = json_encode($return);
        } else {
            $where = array('area_number' => $area_number, 'floor_number' => $floor_number, 'room_number' => $room_number, 'check_inWhether' => 0);
            $this->db->select('bedNumber');
            $this->db->where($where);
            $query = $this->db->get('dorm_bed');
            if ($query->num_rows() > 0) {
                $return_su = array('res' => '40000', 'msg' => '查询成功', 'data' => $query->result());
                $result = json_encode($return_su);
            } else {
                $return_fa = array('res' => '40001', 'msg' => '查询失败', 'data' => '');
                $result = json_encode($return_fa);
            }
        }
        return $result;
    }

    //宿舍修改-领用物品/所有物品查询
    public function bed_article_select($name, $room_number, $bedNumber)
    {
        if ($name == '' || $room_number == '' || $bedNumber == '') {
            $return = array('res' => '40002', 'msg' => '请输入必填字段' ,'data' => '');
            $result = json_encode($return);
        } else {
            $where = array('name' => $name, 'room_number' => $room_number, 'bedNumber' => $bedNumber);
            $select = array('article_number', 'article_description');
            //取领用物品编码 ,领用物品描述
            $this->db->select($select);
            $this->db->where($where);
            $query_article = $this->db->get('dorm_article');
            //取物品信息
            $query_receiving = $this->db->get('dorm_receiving');
            if ($query_article->num_rows()>0 && $query_receiving->num_rows()>0) {
                $return_su = array('res' => '40000', 'msg' => '查询成功', 'data' => $query_article->row(), 'data1' => $query_receiving->result());
                $result = json_encode($return_su);
            } else {
                $return_fa = array('res' => '40001', 'msg' => '查询失败', 'data' => '', 'data1' => $query_receiving->result());
                $result = json_encode($return_fa);
            }
            return $result;
        }
    }

    //床位管理更换床位方法
    public function bed_change($area_number, $floor_number, $room_number, $bedNumber, $jobNumber){
        //必输值未输则报错
        if ($area_number == '' || $floor_number == '' || $room_number== '' || $bedNumber == '' || $jobNumber == '') {
            $return = array('res' => '40002', 'msg' => '信息不全无法更换床位' ,'data' => '');
            $result = json_encode($return);
        } else {
            $query = $this->db->get_where('dorm_bed', array('area_number' => $area_number, 'floor_number' => $floor_number, 'room_number' => $room_number, 'bedNumber' => $bedNumber, 'jobNumber' => $jobNumber));
            //如果取不到值则证明这个床位没有人居住
            if ($query->num_rows() == '') {
                $query_livepeople = $this->db->get_where('dorm_livepeople', array('jobNumber' => $jobNumber));
                $query_bed = $this->db->get_where('dorm_bed', array('area_number' => $area_number, 'floor_number' => $floor_number, 'room_number' => $room_number, 'jobNumber' => $jobNumber));
                $data_bed = array(
                    'idNumber' => '',      //身份证号
                    'name' => '',           //姓名
                    'sex' => '',            //性别
                    'jobNumber' => '',      //工号
                    'department' => '',     //部门
                    'phone' => '',          //联系方式
                    'check_inWhether' => 0  //床位是否住人 1.住人 0.未住人
                );
                $where_bed = array('area_number' => $area_number, 'floor_number' => $floor_number, 'room_number' => $room_number, 'bedNumber' => $query_bed->row()->bedNumber);
                $this->db->where($where_bed);
                $this->db->update('dorm_bed', $data_bed);
                //取床位信息
                $data_livepeople = array(
                    'bedNumber' => $bedNumber//床位号
                );

                //更新床位表数据
                $data2_bed = array(
                    'name' => $query_livepeople->row()->name,               //姓名
                    'sex' => $query_livepeople->row()->sex,                 //性别
                    'idNumber' => $query_livepeople->row()->idNumber,       //身份证号
                    'jobNumber' => $query_livepeople->row()->jobNumber,     //工号
                    'department' => $query_livepeople->row()->department,   //部门
                    'phone' => $query_livepeople->row()->phone,             //联系方式
                    'bedStatus' => 1,                                       //床位状态
                    'check_inWhether' => 1                                  //床位是否住人
                );
                $this->db->where('degsonid', $query_livepeople->row()->degsonid);
                $this->db->update('dorm_livepeople', $data_livepeople);
                $where = array('area_number' => $area_number, 'floor_number' => $floor_number, 'room_number' => $room_number, 'bedNumber' => $bedNumber);
                $this->db->where($where);
                $this->db->update('dorm_bed', $data2_bed);
                $return = $this->db->affected_rows();
                $query_livepeople = $this->db->get_where('dorm_livepeople', array('degsonid' => $query_livepeople->row()->degsonid));
                if ($return) {
                    //床位更换成功
                    $return_su = array('res' => '40000', 'msg' => '床位更换成功', 'data' => $query_livepeople->row()->bedNumber);
                    $result = json_encode($return_su);
                } else {
                    //床位更换失败
                    $return_fa = array('res' => '40001', 'msg' => '床位更换失败', 'data' => '');
                    $result = json_encode($return_fa);
                }
            } else {   //床位有人不允许更换
                $return2 = array('res' => '40001', 'msg' => '床位有人居住不允许调换' ,'data' => '');
                $result = json_encode($return2);
            }
        }

        return $result;
    }

    //床位管理修改方法
    public function bed_update($name, $jobNumber, $phone, $remarks)//$SQ_use
    {   
        // $data_article = array(
        //     'SQ_use' => $SQ_use
        // );

        $data_livepeople = array(
            'phone' => $phone,      //联系方式
            'remarks' => $remarks,  //备注
            // 'SS_use' => $SQ_use
        );
        $where = array('jobNumber' => $jobNumber, 'name' => $name);
        // $where1 = array('SQ_RY_number' => $RY_degsonid, 'SQ_RY_name' => $RY_name);
        // $this->db->where($where1);
        // $this->db->update('dorm_article', $data_article);
        $this->db->where($where);
        $this->db->update('dorm_livepeople' ,$data_livepeople);
        $return = $this->db->affected_rows();
        //床位状态查询
        $this->db->select('bedStatus');
        $this->db->where('jobNumber' ,$jobNumber);
        $query_status = $this->db->get('dorm_bed');
        $query = $this->db->get_where('dorm_livepeople', array('jobNumber' => $jobNumber, 'name' => $name));
        if ($return) {
            //修改成功
            $return_su = array('res' => '40000', 'msg' => '修改成功', 'data' => $query->row(), 'bedStatus' => $query_status->result());
            $result = json_encode($return_su);
        } else {
            //修改失败
            $return_fa = array('res' => '40001', 'msg' => '修改失败' ,'data' => '');
            $result = json_encode($return_fa);
        }
        return $result;
    }

    //床位信息excel导出查询
    public function excel_bed()
    {
        $query = $this->db->query("SELECT A.area_name ,A.floor_name ,A.caveNumber ,B.dorm_floor ,B.room_number ,B.sex ,B.bedNumber ,B.bedStatus ,B.check_inWhether ,B.name ,B.jobNumber ,B.department ,B.phone
                                          ,C.liveDate ,D.room_type FROM dorm_bed  AS B LEFT JOIN dorm_sslmanage AS A ON B.area_number = A.area_number AND B.floor_number = A.floor_number
                                --       LEFT JOIN  dorm_livepeople AS C ON B.RY_idcardnum = C.RY_idcardnum 先注释身份证号有数据再打开
                                         LEFT JOIN dorm_livepeople AS C ON B.jobNumber = C.jobNumber
                                         LEFT JOIN dorm_roominfor AS D ON B.area_number = D.area_number AND B.floor_number = D.floor_number AND B.room_number = D.room_number
                                         ORDER BY A.area_name ,A.floor_name ,B.dorm_floor ,B.room_number ");
        $result = $query->result();
        return json_encode(array('ret' => '40001', 'msg' => $result));
    }
}
